﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace ColorPane
{
    public partial class ColorTracker : UserControl
    {
        private bool _foreColorizing = true;

        public ColorTracker()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            txtR.Left++;
            txtG.Left++;
            txtB.Left++;
            txtR.Left--;
            txtG.Left--;
            txtB.Left--;
            ForeColorizing = _foreColorizing;
        }

        private void SyncColors(ColorSlider cs)
        {
            foreach (Control c in Controls)
            {
                if (c is ColorSlider)
                {
                    if (c != cs) ((ColorSlider)c).RGB = cs.RGB;
                }
            }
            txtR.Text = cs.RGB.R.ToString();
            txtG.Text = cs.RGB.G.ToString();
            txtB.Text = cs.RGB.B.ToString();
            if (_foreColorizing) lblForeColor.BackColor = cs.RGB;
            else lblBackColor.BackColor = cs.RGB;
        }
        [System.ComponentModel.Description("true��ʾ����ǰ��ɫ������Ϊfalse��")]
        public bool ForeColorizing
        {
            get { return _foreColorizing; }
            set
            {
                _foreColorizing = value;
                if (_foreColorizing)
                {
                    csR.RGB = lblForeColor.BackColor;
                }
                else
                {
                    csR.RGB = lblBackColor.BackColor;
                }
                SyncColors(csR);
            }
        }
        [System.ComponentModel.Description("ѡ���ǰ��ɫ��")]
        public Color ForeColorSelection
        {
            get { return lblForeColor.BackColor; }
            set
            {
                lblForeColor.BackColor = value;
                csR.RGB = value;
                _foreColorizing = true;
                SyncColors(csR);
            }
        }
        [System.ComponentModel.Description("ѡ��ı���ɫ��")]
        public Color BackColorSelection
        {
            get { return lblBackColor.BackColor; }
            set
            {
                lblBackColor.BackColor = value;
                csR.RGB = value;
                _foreColorizing = false;
                SyncColors(csR);
            }
        }

        private void SliderScroll(object sender, SliderEventArgs e)
        {
            SyncColors((ColorSlider)sender);
        }

        private void lblForeColor_Click(object sender, EventArgs e)
        {
            ForeColorizing = true;
        }

        private void lblBackColor_Click(object sender, EventArgs e)
        {
            ForeColorizing = false;
        }

        private void lblSwitch_Click(object sender, EventArgs e)
        {
            Color temp = lblForeColor.BackColor;
            lblForeColor.BackColor = lblBackColor.BackColor;
            lblBackColor.BackColor = temp;
            ForeColorizing = _foreColorizing;
        }

        private void lblGray_Click(object sender, EventArgs e)
        {
            lblBackColor.BackColor = Color.White;
            lblForeColor.BackColor = Color.Black;
            ForeColorizing = _foreColorizing;
        }
    }
}
